package com.spdzen.controller.admin;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spdzen.model.News;
import com.spdzen.service.NewsService;
import com.spdzen.support.AdminLoginUtils;
import com.spdzen.support.LogUtils;
import com.spdzen.support.PageOffsetParam;
import com.spdzen.support.SimpleStringUtils;
import com.spdzen.support.UploadFileUtils;
import com.spdzen.support.result.JsonResult;
import com.spdzen.support.validation.AddValidGroup;
import com.spdzen.support.validation.UpdateValidGroup;
import com.spdzen.vo.search.NewsAdminSearch;

@Controller
@RequestMapping("/admin/news")
public class NewsController {
	@Resource
	private NewsService newsService;
	
	@RequestMapping("/pageView.do")
	public String pageView() {
		return "/admin/news/newsPageView";
	}
	
	@RequestMapping("/addView.do")
	public String addTextView() {
		return "/admin/news/addNewsView";
	}
	
	@RequestMapping("/editView.do")
	public String editView(Long id, HttpServletRequest request) {
		if(id != null && id.longValue() > 0){
			request.setAttribute("id", id);
		}
		return "/admin/news/editNewsView";
	}
	
	@RequestMapping("/queryPage.do")
	@ResponseBody
	public JsonResult queryPage(PageOffsetParam param, NewsAdminSearch search) {
		search.setTitle(SimpleStringUtils.likeSqlString(search.getTitle()));
		
		try {
			return JsonResult.success(newsService.selectPage(param, search));
		} catch (Exception e) {
			LogUtils.logError(e, param, search);
			return JsonResult.error();
		}
	}
	
	@RequestMapping(path = {"/add.do"}, method = {RequestMethod.POST})
	@ResponseBody
	public JsonResult add(@Validated({AddValidGroup.class})News record, BindingResult br, HttpServletRequest request){
		if(br.hasErrors()){
			return JsonResult.errorParameter();
		}
		
		record.setOperateId(AdminLoginUtils.getUserId());
		record.setOperateTime(new Date());
		
		try {
			String coverUrl = UploadFileUtils.copyToDirectory(request, record.getCoverImage(), UploadFileUtils.NEWS_IMAGE_PATH);
			record.setCoverImage(coverUrl);
			
			newsService.insert(record);
			return JsonResult.success();
		} catch (Exception e) {
			LogUtils.logError(e, record);
			return JsonResult.error();
		}
	}
	
	@RequestMapping(path = {"/edit.do"}, method = {RequestMethod.POST})
	@ResponseBody
	public JsonResult edit(@Validated(value = {UpdateValidGroup.class}) News record, BindingResult br){
		if(br.hasErrors()){
			return JsonResult.errorParameter();
		}
		
		try {
			newsService.updateById(record);
			return JsonResult.success();
		} catch (Exception e) {
			LogUtils.logError(e, record);
			return JsonResult.error();
		}
	}
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public JsonResult delete(Long id){
		if(id == null || id.longValue() < 1){
			return JsonResult.errorParameter();
		}
		
		try {
			newsService.deleteById(id);
			return JsonResult.success();
		} catch (Exception e) {
			LogUtils.logError(e, id);
			return JsonResult.error();
		}
	}
	
	@RequestMapping("/queryById.do")
	@ResponseBody
	public JsonResult queryById(Long id){
		if(id == null || id.longValue() < 1){
			return JsonResult.errorParameter();
		}
		
		try {
			return JsonResult.success(newsService.selectAdminVOById(id));
		} catch (Exception e) {
			LogUtils.logError(e, id);
			return JsonResult.error();
		}
	}
}
